package com.mdxq.mall.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    public static final String EXCHANGE_NAME = "normal_exchange";//交换机的名字
    public static final String QUEUE_NAME = "normal_queue";
    public static final String DLX_EXCHANGE_NAME = "dlx_exchange1";//死信交换机
    public static final String DLX_QUEUE_NAME = "dlx_queue1";//死信队列
    @Bean("dlxExchange1")
    public Exchange dlxExchange(){
        return ExchangeBuilder.topicExchange(DLX_EXCHANGE_NAME).durable(true).build();
    }
    @Bean("dlxQueue1")
    public Queue dlxQueue(){
        return QueueBuilder.durable(DLX_QUEUE_NAME).build();
    }
    @Bean
    public Binding dlxbinding(@Qualifier("dlxExchange1") Exchange exchange,@Qualifier("dlxQueue1")Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("dlx.*").noargs();
    }
    @Bean("normalQueue")
    public Queue queue(){
        return QueueBuilder.durable(QUEUE_NAME)//是否持久话
                .expires(1000*60*60*24)//队列连续24小时未使用，就销毁
                .ttl(10*1000)//十秒后进入死信队列
//                .ttl(60*30*1000)//设置消息最多保存30分钟（30分钟内未被消费）
                .deadLetterExchange(DLX_EXCHANGE_NAME)//死信的队列
                .deadLetterRoutingKey("dlx.info")//死信的路由键
                .build();
    }
    @Bean("normalExchange")
    public Exchange exchange(){
        //创建一个能持久化消息的主题交换机
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }
    @Bean
    public Binding binding(@Qualifier("normalExchange") Exchange exchange,@Qualifier("normalQueue")Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("*.info").noargs();
    }
}
